﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FlashCards.SL.FlashCardCreator.Utils;

namespace FlashCards.SL.FlashCardCreator.Views
{
    public partial class FlashcardPartView : UserControl
    {
        bool _isInSelectMode;
        bool _usingArrowKeys;
        AccentParameters _accentParams;
        int _lastSelectionStart;

        Dictionary<string, List<string>> _accentsDictionary;

        public FlashcardPartView()
        {
            if (Application.Current.Resources.Contains("AccentsLettersDictionary"))
            {
                _accentsDictionary = Application.Current.Resources["AccentsLettersDictionary"] as Dictionary<string, List<string>>;
            }

            InitializeComponent();
        }

        bool LetterHasAccents(char letter)
        {
            _accentsDictionary = Application.Current.Resources["AccentsLettersDictionary"] as Dictionary<string, List<string>>;
            return _accentsDictionary.ContainsKey(letter.ToString());
        }

        void InitializeAccentsPanel(char character, bool isSelectMode)
        {
            AccentLettersPanel.Children.Clear();

            var letter = character.ToString();
            var list = _accentsDictionary[letter];
            foreach (var item in list)
            {
                var btn = new Button();
                btn.Content = item;
                btn.Width = 15;
                btn.KeyDown += Button_KeyDown;
                btn.Click += Button_Click;
                btn.LostFocus += Button_LostFocus;
                btn.Margin = new System.Windows.Thickness(1);
                btn.FontSize = 14;

                var command = DataContext.GetType().GetProperty("ReplaceAccentCharacterCommand").GetValue(DataContext, null);

                btn.Command = command as ICommand;

                AccentLettersPanel.Children.Add(btn);
                
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox == null)
                return;

            if (string.IsNullOrEmpty(textBox.Text))
                return;

            var lastLetter = textBox.Text[textBox.SelectionStart - 1];

            if (LetterHasAccents(lastLetter))
            {
                _accentParams = new AccentParameters(lastLetter, ' ', textBox.SelectionStart - 1);
                InitializeAccentsPanel(lastLetter, false);
                AccentLettersPanel.Visibility = System.Windows.Visibility.Visible;
            }
            else
                AccentLettersPanel.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox == null)
                return;

            if (string.IsNullOrEmpty(textBox.Text))
                return;

            _lastSelectionStart = textBox.SelectionStart;

            if (textBox.SelectedText.Length == 1)
            {
                _isInSelectMode = true;
                var lastLetter = textBox.SelectedText[0];

                if (LetterHasAccents(lastLetter))
                {
                    _accentParams = new AccentParameters(lastLetter, ' ', textBox.SelectionStart);
                    InitializeAccentsPanel(lastLetter, true);
                    AccentLettersPanel.Visibility = System.Windows.Visibility.Visible;
                }
                else
                    AccentLettersPanel.Visibility = System.Windows.Visibility.Collapsed;
            }
            else if (_isInSelectMode)
            {
                AccentLettersPanel.Visibility = System.Windows.Visibility.Collapsed;
                _isInSelectMode = false;
            }
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.Key == Key.Tab && AccentLettersPanel.Visibility == System.Windows.Visibility.Visible)
            //{
            //    var btn = AccentLettersPanel.Children[0] as Button;
            //    if (btn != null)
            //    {
            //        btn.Focus();
            //    }
            //}
        }

        private void Button_KeyDown(object sender, KeyEventArgs e)
        {
            _usingArrowKeys = false;
            if (e.Key == Key.Left || e.Key == Key.Right)
            {
                var btn = sender as Button;
                if (btn != null)
                {
                    _usingArrowKeys = true;
                    var index = AccentLettersPanel.Children.IndexOf(btn);
                    Button changedButton = null;
                    if (e.Key == Key.Left && index > 0)
                    {
                        changedButton = AccentLettersPanel.Children[index - 1] as Button;
                    }
                    else if (e.Key == Key.Left && index == 0)
                    {
                        changedButton = AccentLettersPanel.Children[AccentLettersPanel.Children.Count - 1] as Button;
                    }
                    else if (e.Key == Key.Right && index < AccentLettersPanel.Children.Count - 1)
                    {
                        changedButton = AccentLettersPanel.Children[index + 1] as Button;
                    }
                    else if (e.Key == Key.Right && index == AccentLettersPanel.Children.Count - 1)
                    {
                        changedButton = AccentLettersPanel.Children[0] as Button;
                    }

                    changedButton.Focus();
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _usingArrowKeys = false;
            var btn = sender as Button;
            if (btn.Command != null)
            {
                _accentParams.NewCharacter = btn.Content.ToString()[0];

                btn.Command.Execute(_accentParams);
            }
            InputTextBox.Focus();
            InputTextBox.SelectionStart = _lastSelectionStart;
            InputTextBox.SelectionLength = 0;
            AccentLettersPanel.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void Button_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!_usingArrowKeys)
            {
                InputTextBox.Focus();
            }
        }
    }
}
